<style>
.pl30{
    padding-left: 30px;
}
.mb20{
    margin-bottom: 20px;
}
</style>
<template>
    <div>
        <product-title></product-title>
        <div class="wrap pl30">
            <!-- <Circle :percent="percent" :stroke-color="color">
                <Icon v-if="percent == 100" type="ios-checkmark-empty" size="60" style="color:#5cb85c"></Icon>
                <span v-else style="font-size:24px">{{ percent }}%</span>
            </Circle> -->
            <Form  :label-width="160">
                <Col  v-if="yid==83 || yid==72">
                    <FormItem label="上月考核保单更新：">
                        <Button type="primary" :disabled='canclick' @click="syncuserinfo1">上月考核保单更新</Button>
                    </FormItem>
                    <FormItem label="上月员工晋升考核：">
                        <Button type="primary" :disabled='canclick' @click="syncuserinfo2">上月员工晋升考核</Button>
                    </FormItem>
                    <FormItem label="上月员工津贴更新：">
                        <Button type="primary" :disabled='canclick' @click="syncuserinfo0">上月员工津贴更新</Button>
                    </FormItem>
                    <FormItem label="上月员工薪资考核更新: ">
                        <Button type="primary" :disabled='canclick' @click="syncuserinfo3">上月员工薪资考核更新</Button>
                    </FormItem>
                </Col>
                
                <FormItem label="同步员工数据：">
                    <Button type="primary" :disabled='canclick||(progress != 0 && progress != 100)' @click="syncuserinfo">同步员工数据</Button>
                </FormItem>
                <!-- <FormItem label="更新员工基本信息：">
                    <Button type="primary" :disabled='canclick' @click="updateBaseinfo">更新基本信息</Button>
                </FormItem> -->
                <!-- <FormItem label="计算员工业绩：">
                        <Row>
                            <Col span="3" >
                                <DatePicker type="month" :options="options" placeholder="选择更新月份" @on-change="dateChange($event)" v-model="fycdatetime"></DatePicker>
                            </Col>
                            <Col span="3" offset="1">
                                <Button type="primary" :disabled='canclick' @click="updateFYC">计算员工业绩</Button>
                            </Col>
                        </Row>
                    
                </FormItem>
                <FormItem label="计算员工继续率：">
                        <Row>
                            <Col span="3" >
                                <DatePicker type="month" :options="options" placeholder="选择更新月份" @on-change="perdateChange($event)" v-model="Persistencydatetime"></DatePicker>
                            </Col>
                            <Col span="3" offset="1">
                                <Button type="primary" :disabled='canclick' @click="updateper">计算员工继续率</Button>
                            </Col>
                        </Row>
                    
                </FormItem>
                <FormItem label="计算团队业绩：">
                    <Button type="primary" :disabled='canclick' @click="updateteam">计算团队业绩</Button>
                </FormItem> -->
                <FormItem label="考核：">
                    <Button type="primary" :disabled='canclick' @click="updatecheck">考核</Button>
                </FormItem>
            </Form>
            <h3 class="mb20">
                更新详情:
            </h3>
            <!-- <div>{{onetext}}</div>
            <div>{{twotext}}</div>
            <div>{{threetext}}</div> -->
            {{synctxt}}
            <p v-show="progress != 0">
                
                {{progress != 100 ? '同步员工数据:同步中……'+progress+'%' : '同步员工数据：已完成！'}}
            </p>
            <p>
                {{baseinfotxt}}
            </p>
            <p>
                {{fyctxt}}
            </p>
            <p>
                {{pertxt}}
            </p>
            <p>
                {{teamtxt}}
            </p>
            <p>
                {{checktxt}}
            </p>
        </div>
        <!-- <Button type="primary"  @click="clickButton">考核</Button>
        {{sockettxt}} -->
    </div>
</template>
<script>
import productTitle from '../../components/productTitle'
import { getAllUid  , getAllUidRank , getAllId , getAllIdInvert , synchData} from '../../api/employee'
import { updateBasicInfo , updateFyc , updatePersistency , setCheckData , computeCheck } from '../../api/basiclaw'
import { formatTime } from '../../libs/time'
import {get_policy_count,change_policy2,get_emp_count,change_salary2,check_emp2,get_policy_salary_count,deal_allowance} from '../../api/policycount.js'



export default {
    data () {
        return {
            options: {
                disabledDate (date) {
                    let _date = formatTime(new Date(),'yyyy-MM')
                    return date && date.valueOf() >= (new Date(_date).getTime())
                }
            },
            yid:'0',
            countname:"",
            num:1,
            onetext:"",
            sockettxt:'',
            empcount1:1,
            empcount2:1,
            empcounts:1,//zongshu
            empcounts2:1,
            empname:"",

            allowancecounts:1,//'员工津贴'
            twotext:"",

            threetext:"",
            countnum:1,
            userlistasc:[],
            userlist:[],
            userlistrank:[],
            tagindex:0,
            current:0,
            canclick:false,
            fycdate:'',
            fycdatetime:'',
            Persistencydate:'',
            Persistencydatetime:'',
            synctxt:'',
            baseinfotxt:'',
            fyctxt:'',
            pertxt:'',
            teamtxt:'',
            checktxt:'',
            progress:0
        }
    },
    computed: {
        color () {
            let color = '#2db7f5';
            if (this.percent == 100) {
                color = '#5cb85c';
            }
            return color;
        },
        percent() {
            return this.$store.state.socket.synch_data ? this.$store.state.socket.synch_data : 0
        }
    },
    // sockets:{
    //     connect: function(){
    //         console.log('socket connected')
    //     },
    //     insert: function(val){
    //         console.log(val)
    //     }
    // },
    components: {
        productTitle,
        
    }, 
    //channel: 'insert.2',
    // echo: {
    //     'connect': (payload, vm) => {
    //         console.log('blog post created', payload);
    //     },
    //     'insert': (payload, vm) => {
    //         console.log('blog post deleted', payload);
    //     }
    // }, 
    channel: 'progress.'+sessionStorage.getItem('id'),
    mounted () {
        console.log(this.$echo)
        let _id = 'progress.'+sessionStorage.getItem('id')
        this.$echo.channel(_id).listen('ProgreeNotice', (payload) => {
            console.log(payload);
            this.$nextTick(() => {
                setTimeout(() => {
                    this.progress = payload.progress
                },200)
            })
             this.$store.dispatch('changesocket', payload);
        });
    },
    created () {
        this.getUid()
        this.getlist()
        this.yid=sessionStorage.getItem('id');
        let _time = new Date(formatTime(new Date(),'yyyy-MM'))
        let _date =formatTime(new Date(_time),'yyyy-MM')
        this.fycdate = _date
        this.fycdatetime = _date
        this.Persistencydate = _date
        this.Persistencydatetime = _date
    },

    methods: {
        clickButton: function(val){
            // $socket is socket.io-client instance
            console.log(123)
            console.log(this.$socket)
            this.$socket.emit('emit_method', '123');
        },
        getlist(){
            new Promise((resolve, reject) => {
                get_policy_count().then(response => {
                    console.log(response.data,"..response.data")
                    if(response.data.err == 0){
                        this.countnum =response.data.data.count
                        console.log(this.countnum,"...保单.")
                    } else {
                       this.$Message.error(response.data.info);
                    }                       
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
            new Promise((resolve, reject) => {
                get_emp_count().then(response => {
                    console.log(response.data,"..response.data")
                    if(response.data.err == 0){
                        this.empcounts =response.data.data.count
                        this.empcounts2 =response.data.data.count 
                        console.log(this.empcounts,"晋升....考核更新",this.empcounts2 )
                    } else {
                       this.$Message.error(response.data.info);
                    }                       
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
            new Promise((resolve, reject) => {
                get_policy_salary_count().then(response => {
                    if(response.data.err == 0){
                        this.allowancecounts =response.data.data.count;
                        console.log(this.allowancecounts,"上月员工津贴更新")
                    } else {
                       this.$Message.error(response.data.info);
                    }                       
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        syncuserinfo0(){
            let _userlistasc = this.allowancecounts
            console.log(_userlistasc)
            if(_userlistasc > 0){
                this.canclick = true
                this.sync(_userlistasc)
            }else{
                this.$Message.error('请认真核对员工津贴是否存在');
            }
        },
        sync(_userlistasc){
            if(this.num >_userlistasc){
                this.synctxt = '上月员工津贴更新：上月员工津贴已完成全部更新'
                this.num = 1
                this.canclick = false
                return;  
            }
            let _params = {
                num : this.num
            }
            new Promise((resolve, reject) => {
                deal_allowance(_params).then(response => {
                        this.num++
                        this.synctxt = '上月员工津贴更新：'+this.num+'/'+_userlistasc+'已完成更新'
                        this.sync(_userlistasc)
                    resolve();
                }).catch(error => {
                    this.canclick = false
                    reject(error);
                });
            });
        },
        // 点击第一个上月考核保单更新
        syncuserinfo1(){
            let _userlistasc = this.countnum
            console.log(_userlistasc)
            if(_userlistasc > 0){
                this.canclick = true
                this.syncinfoa(_userlistasc)
            }else{
                this.$Message.error('请认真核对保单是否存在');
            }
        },
        syncinfoa(_userlistasc){
            if(this.num >_userlistasc){
                this.synctxt = '上月考核保单：上月考核保单已完成全部更新'
                this.num = 1
                this.canclick = false
                return;  
            }
            let _params = {
                num : this.num
            }
            new Promise((resolve, reject) => {
                change_policy2(_params).then(response => {
                        this.num++
                        this.synctxt = '上月考核保单：'+this.num+'/'+_userlistasc+'已完成更新'
                        this.syncinfoa(_userlistasc)
                    resolve();
                }).catch(error => {
                    this.canclick = false
                    reject(error);
                });
            });
        },
        // 点击第二个
        syncuserinfo2(){
            console.log(this.empcounts,"this.empcounts.....")
            let _userlistasc2 = this.empcounts
            console.log(_userlistasc2)
            if(_userlistasc2 > 0){
                this.canclick = true
                this.syncinfob(_userlistasc2)
            }else{
                this.$Message.error('请认真核对员工晋升是否存在');
            }
        },
        syncinfob(_userlistasc2){
            if(this.empcount1 >_userlistasc2){
                this.synctxt = '上月员工晋升考核：上月员工晋升考核已完成全部更新'
                this.empcount1 = 1
                this.canclick = false
                return;  
            }
            let _params = {
                num : this.empcount1
            }
            new Promise((resolve, reject) => {
                check_emp2(_params).then(response => {
                        this.empcount1++
                        this.synctxt = '上月员工晋升考核：'+this.empcount1+'/'+_userlistasc2+'已完成更新'
                        this.syncinfob(_userlistasc2)
                                  
                    resolve();
                }).catch(error => {
                    this.canclick = false
                    reject(error);
                });
            });
        },
        // 点击第三个
        syncuserinfo3(){
            console.log(this.empcounts2,"this.empcounts2.....")
            let _userlistasc3 = this.empcounts2
            console.log(_userlistasc3)
            if(_userlistasc3 > 0){
                this.canclick = true
                this.syncinfoc(_userlistasc3)
            }else{
                this.$Message.error('请认真核对员工薪资是否存在');
            }
        },
        syncinfoc(_userlistasc3){
            if(this.empcount2 >_userlistasc3){
                this.synctxt = '上月员工薪资考核更新：上月员工薪资考核已完成全部更新'
                this.empcount2 = 1
                this.canclick = false
                return;  
            }
            let _params = {
                num : this.empcount2
            }
            new Promise((resolve, reject) => {
                change_salary2(_params).then(response => {
                        this.empcount2++
                        this.synctxt = '上月员工薪资考核更新：'+this.empcount2+'/'+_userlistasc3+'已完成更新'
                        this.syncinfoc(_userlistasc3)
                    resolve();
                }).catch(error => {
                    this.canclick = false
                    reject(error);
                });
            });
        },
        dateChange(date){
            this.fycdate = date
        },
        perdateChange(date){
            this.Persistencydate =date
        },
        //获取全部员工 正序
        getUid(){           
            new Promise((resolve, reject) => {
                getAllId().then(response => {
                    if(response.data.err == 0){
                        this.userlistasc = response.data.data
                    } else {
                       this.$Message.error(response.data.info);
                    }                       
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
            new Promise((resolve, reject) => {
                getAllIdInvert().then(response => {
                    if(response.data.err == 0){
                        this.userlist = response.data.data
                    } else {
                       this.$Message.error(response.data.info);
                    }                       
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
            // new Promise((resolve, reject) => {
            //     getAllUidRank().then(response => {
            //         if(response.data.err == 0){
            //             this.userlistrank = response.data.data
            //         } else {
            //            this.$Message.error(response.data.info);
            //         }                       
            //         resolve();
            //     }).catch(error => {
            //         reject(error);
            //     });
            // });
            
        },
        //点击同步员工数据
        syncuserinfo(){
            let _userlistasc = this.userlistasc
            console.log(_userlistasc)
            if(_userlistasc.length > 0){
                this.canclick = true
                this.syncinfo(_userlistasc)
            }else{
                this.$Message.error('员工数据异常');
            }
        },
        //同步员工数据
        syncinfo(_userlistasc){
            
            new Promise((resolve, reject) => {
                synchData().then(response => {
                    if(response.data.err == 0){
                         this.canclick = false
                         this.synctxt = ''
                    } else {
                        this.synctxt = '同步员工数据：操作失败，请重试。'
                        this.current = 0
                        this.canclick = false
                        this.$Message.error(response.data.info);
                    }                       
                    resolve();
                }).catch(error => {
                    this.canclick = false
                    reject(error);
                });
            });
        },
        //点击更新基本信息
        updateBaseinfo(){
            let _userlist = this.userlist
            if(_userlist.length > 0){
                this.canclick = true
                this.updateinfo(_userlist)
            }else{
                this.$Message.error('员工数据异常');
            }
        },
        //更新基本信息
        updateinfo(_userlist){
            let _length = _userlist.length
            if(this.current >=_userlist.length){
                this.baseinfotxt = '更新基本信息：已完成全部更新'
                this.current = 0
                this.canclick = false
                return;  
            }
            let _params = {
                eid : _userlist[this.current]    
            }
            new Promise((resolve, reject) => {
                updateBasicInfo(_params).then(response => {
                    if(response.data.err == 0){
                        this.baseinfotxt = '更新基本信息：'+this.current+'/'+_length+'已完成更新'
                        this.current++
                        this.updateinfo(_userlist)

                    } else {
                        this.canclick = false
                        this.$Message.error(response.data.info);
                    }                       
                    resolve();
                }).catch(error => {
                    this.canclick = false
                    reject(error);
                });
            });
        },
         //点击更新员工业绩
        updateFYC(){
            let _userlistrank = this.userlistrank
            if(_userlistrank.length > 0){
                if(!this.fycdate){
                    this.$Message.error('请选择年月')
                     
                }else{
                    this.canclick = true
                    this.updateuserfyc(_userlistrank)
                }
            }else{
                this.$Message.error('员工数据异常');
            }
        },
        updateuserfyc(_userlistrank){
            let _Length = _userlistrank.length
            if(this.current < _Length){
                this.teamtxt = '计算员工业绩：'+this.current+'/'+_Length+'已完成更新'
                let _ranklist = _userlistrank[this.current]
                //console.log(_ranklist)
                //this.rankupdata(_ranklist)
                let _rank= _ranklist.rank
                let _arry = _ranklist.data
                console.log(_arry.length)
                let _length = _arry.length
                if(_length > 0){
                    this.personalajax(_rank,_arry,_length)
                }
            }else{
                this.teamtxt = '计算员工业绩：已完成全部更新'
                this.current = 0
                this.tagindex = 0
                this.canclick = false
                return 
            }
        },
        personalajax(_rank,_arry,_length){
            let _params = {
                date:this.fycdate,
                uid:_arry[this.tagindex]
                // rank:114,
                // uid:6403
            }
            let _len = 0
            new Promise((resolve, reject) => {
                updateFyc(_params).then(response => {
                    console.log(response.data)
                    if(response.data.err == 0){
                        this.tagindex ++
                        if(this.tagindex < _length){
                            this.personalajax(_rank,_arry,_length)
                        }
                        else if(this.tagindex == _length){
                            this.current ++
                            this.tagindex = 0
                            this.updateuserfyc(this.userlistrank)
                        } else{
                            return 
                        } 
                    } else {
                        this.current = 0
                        this.canclick = false
                        this.$Message.error(response.data.info);
                    }                       
                    resolve();
                }).catch(error => {
                    this.canclick = false
                    reject(error);
                });
            });
        },

        //点击计算继续率
        updateper(){
            let _userlist = this.userlist
            if(_userlist.length > 0){
                if(!this.Persistencydate){
                    this.$Message.error('请选择年月')
                     
                }else{
                    this.canclick = true
                    this.updateuserper(_userlist)
                }
                
            }else{
                this.$Message.error('员工数据异常');
            }
        },
        //计算继续率
        updateuserper(_userlist){
            let _length = _userlist.length
            if(this.current >=_userlist.length){
                this.pertxt = '计算员工继续率：已完成全部更新'
                this.current = 0
                this.canclick = false
                return;  
            }
            
            let _params = {
                uid : _userlist[this.current],
                date: this.Persistencydate
            }
            //console.log(_params)
            new Promise((resolve, reject) => {
                updatePersistency(_params).then(response => {
                    console.log(response.data)
                    if(response.data.err == 0){
                        //this.prolist = response.data.data
                        this.pertxt = '计算员工继续率：'+this.current+'/'+_length+'已完成更新'
                        this.current++
                        this.updateuserper(_userlist)

                    } else {
                        this.canclick = false
                        this.$Message.error(response.data.info);
                    }                       
                    resolve();
                }).catch(error => {
                    this.canclick = false
                    reject(error);
                });
            });
        },
        //点击计算团队业绩
        updateteam(){
            let _userlistrank = this.userlistrank
            console.log(_userlistrank)
            if(_userlistrank.length > 0){
                this.canclick = true
                this.updateteamdata(_userlistrank)
            }else{
                this.$Message.error('员工数据异常');
            }
        },
        updateteamdata(_userlistrank){
            let _Length = _userlistrank.length
            if(this.current < _Length){
                this.teamtxt = '计算团队业绩：'+this.current+'/'+_Length+'已完成更新'
                let _ranklist = _userlistrank[this.current]
                //console.log(_ranklist)
                //this.rankupdata(_ranklist)
                let _rank= _ranklist.rank
                let _arry = _ranklist.data
                console.log(_arry.length)
                let _length = _arry.length
                if(_length > 0){
                    this.rankajax(_rank,_arry,_length)
                }
            }else{
                this.teamtxt = '计算团队业绩：已完成全部更新'
                this.current = 0
                this.tagindex = 0
                this.canclick = false
                return 
            }
        },
        rankajax(_rank,_arry,_length){
            let _params = {
                rank:_rank,
                uid:_arry[this.tagindex]
                // rank:114,
                // uid:6403
            }
            let _len = 0
            new Promise((resolve, reject) => {
                setCheckData(_params).then(response => {
                    console.log(response.data)
                    if(response.data.err == 0){
                        this.tagindex ++
                        if(this.tagindex < _length){
                            this.rankajax(_rank,_arry,_length)
                        }
                        else if(this.tagindex == _length){
                            this.current ++
                            this.tagindex = 0
                            this.updateteamdata(this.userlistrank)
                        } else{
                            return 
                        } 
                    } else {
                        this.current = 0
                        this.canclick = false
                        this.$Message.error(response.data.info);
                    }                       
                    resolve();
                }).catch(error => {
                    this.canclick = false
                    reject(error);
                });
            });
        },


        //点击考核
        updatecheck(){
            let _userlist = this.userlist
            console.log(_userlist)
            if(_userlist.length > 0){
                this.canclick = true
                this.checkajax(_userlist)
            }else{
                this.$Message.error('员工数据异常');
            }
        },
        //考核员工数据
        checkajax(_userlist){
            let _length = _userlist.length
            if(this.current >=_userlist.length){
                this.checktxt = '考核员工数据：已完成全部更新'
                this.current = 0
                this.canclick = false
                return;  
            }
            let _params = {
                eid : _userlist[this.current]    
            }
            new Promise((resolve, reject) => {
                computeCheck(_params).then(response => {
                    if(response.data.err == 0){
                        this.checktxt = '考核员工数据：'+this.current+'/'+_length+'已完成更新'
                        this.current++
                        this.checkajax(_userlist)

                    } else {
                        this.canclick = false
                        this.$Message.error(response.data.info);
                    }                       
                    resolve();
                }).catch(error => {
                    this.canclick = false
                    reject(error);
                });
            });
        },






        //点击考核
        // updatecheck(){
        //     let _userlistrank = this.userlistrank
        //     if(_userlistrank.length > 0){
        //         this.canclick = true
        //         this.updateCheckData(_userlistrank)
        //     }else{
        //         this.$Message.error('员工数据异常');
        //     }
        // },
        // updateCheckData(_userlistrank){
        //     let _Length = _userlistrank.length
        //     if(this.current < _Length){
        //         this.checktxt = '考核：'+this.current+'/'+_Length+'已完成更新'
        //         let _ranklist = _userlistrank[this.current]
        //         //console.log(_ranklist)
        //         //this.rankupdata(_ranklist)
        //         let _rank= _ranklist.rank
        //         let _arry = _ranklist.data
        //         let _length = _arry.length
        //         if(_length > 0){
        //             this.checkajax(_rank,_arry,_length)
        //         }
        //     }else{
        //         this.checktxt = '考核：已完成全部更新'
        //         this.current = 0
        //         this.tagindex = 0
        //         this.canclick = false
        //         return 
        //     }
        // },
        // checkajax(_rank,_arry,_length){
        //     let _params = {
        //         rank:_rank,
        //         uid:_arry[this.tagindex]
        //     }
        //     let _len = 0
        //     new Promise((resolve, reject) => {
        //         computeCheck(_params).then(response => {
        //             console.log(response.data)
        //             if(response.data.err == 0){
        //                 this.tagindex ++
        //                 if(this.tagindex < _length){
        //                     this.checkajax(_rank,_arry,_length)
        //                 }
        //                 else if(this.tagindex == _length){
        //                     this.current ++
        //                     this.tagindex = 0
        //                     this.updateCheckData(this.userlistrank)
        //                 } else{
        //                     return 
        //                 } 
        //             } else {
        //                 this.current = 0
        //                 this.canclick = false
        //                 this.$Message.error(response.data.info);
        //             }                       
        //             resolve();
        //         }).catch(error => {
        //             this.canclick = false
        //             reject(error);
        //         });
        //     });
        // },
    }
}
</script>

